package com.culture.config.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.culture.config.util.AuthenticationUtil;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

@PropertySource(value = {"classpath:/properties/swagger3.properties"})
@Configuration
@EnableOpenApi
public class Swagger3Config {
	@Value("${springfox.documentation.swagger-ui.base-url}")
	private String swaggerBasePrex;
	
	@Value("${swagger.enabled}")
    private Boolean enable;

    @Value("${swagger.application.name}")
    private String applicationName;

    @Value("${swagger.application.version}")
    private String applicationVersion;

    @Value("${swagger.application.description}")
    private String applicationDescription;

    @Value("${swagger.application.group.name}")
    private String applicationGroupName;

    @Value("${swagger.application.apis.selector}")
    private String selector;
    
	@Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)	
                .apiInfo(apiInfo())
                .pathMapping("/")
                .groupName(applicationGroupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(selector))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security())
                .securityContexts(securityContexts())
                .globalRequestParameters(globalRequestParameters());
        		
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName)
                .description(applicationDescription)
                .version(applicationVersion)
                .build();
    }
    
       
    private List<SecurityScheme> security(){
    	List<SecurityScheme> schemeList = new LinkedList<>();
    	schemeList.add(new BasicAuth("basicAuth"));    	
    	schemeList.add(new ApiKey(AuthenticationUtil.BASIC_AUTH_KEY_NAME,AuthenticationUtil.BASIC_AUTH_KEY_NAME,"header"));    	
    	schemeList.add(new ApiKey(AuthenticationUtil.CRSF_TOKEN_NAME,AuthenticationUtil.CRSF_TOKEN_NAME,"header"));    	
    	return schemeList;
    }   
    
    
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).build());
        return securityContexts;
    }
 
    private List<SecurityReference> defaultAuth() {
    	List<SecurityReference> securityReferences = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[] {authorizationScope};  
        securityReferences.add(new SecurityReference(AuthenticationUtil.BASIC_AUTH_KEY_NAME, authorizationScopes));
        return securityReferences;
    }
    
    private List<RequestParameter> globalRequestParameters() {
    	return Collections.emptyList();
    }
    
    public String getSwaggerPath(String params) {
    	String path = StringUtils.join(swaggerBasePrex, "/swagger-ui/index.html");    	
    	if(StringUtils.isNotBlank(params))		{
    		path = path.concat("?").concat(params);
    	}
    	return path;
    }
}
